library(rstan)
library(plyr)
library(ggplot2)
library(gridExtra)
library(matrixStats)
library(stargazer)

# Set to the local directory of the replication data
setwd("~/Downloads/Replication_Data/")

source("ggplot_theme.R")

# Load long-formatted data
L2016 <- readRDS("Data/CCES-L2016.rds")
L2016D <- readRDS("Data/CCES-L2016D.rds")
L2016R <- readRDS("Data/CCES-L2016R.rds")

L2012 <- readRDS("Data/CCES-L2012.rds")
L2012D <- readRDS("Data/CCES-L2012D.rds")
L2012R <- readRDS("Data/CCES-L2012R.rds")

L2008 <- readRDS("Data/CCES-L2008.rds")
L2008D <- readRDS("Data/CCES-L2008D.rds")
L2008R <- readRDS("Data/CCES-L2008R.rds")

# Load posterior distributions of sigma (5-10 minutes to load on a 2018 laptop)
posterior_cces_2016 <- as.matrix(readRDS("Fitted_Models/posterior_cces_2016.rds"))
posterior_cces_2016D <- as.matrix(readRDS("Fitted_Models/posterior_cces_2016D.rds"))
posterior_cces_2016R <- as.matrix(readRDS("Fitted_Models/posterior_cces_2016R.rds"))

posterior_cces_2012 <- as.matrix(readRDS("Fitted_Models/posterior_cces_2012.rds"))
posterior_cces_2012D <- as.matrix(readRDS("Fitted_Models/posterior_cces_2012D.rds"))
posterior_cces_2012R <- as.matrix(readRDS("Fitted_Models/posterior_cces_2012R.rds"))

posterior_cces_2008 <- as.matrix(readRDS("Fitted_Models/posterior_cces_2008.rds"))
posterior_cces_2008D <- as.matrix(readRDS("Fitted_Models/posterior_cces_2008D.rds"))
posterior_cces_2008R <- as.matrix(readRDS("Fitted_Models/posterior_cces_2008R.rds"))

# Get estimates of sigma for each actor in the CCES datasets
get_parameters <- function(posterior, data, year_name, partyID) {

  G <- unique(data[, c("name", "j")])
  G$year <- year_name
  G$zeta <- apply(posterior[, grepl("zeta\\[", colnames(posterior))], 2, median)

  G$sigma <- apply(posterior[, grepl("sigma\\[", colnames(posterior))], 2, median)
  G$lower90 <- apply(posterior[, grepl("sigma\\[", colnames(posterior))], 2, quantile, 0.05)
  G$upper90 <- apply(posterior[, grepl("sigma\\[", colnames(posterior))], 2, quantile, 0.95)
  G$sd <- by(data$y, data$j, sd)
  G$n <- by(data$y, data$j, length)
  G$n_r <- by(data$partyID, data$j, function(x) sum(x == "Republican", na.rm = TRUE))
  G$n_d <- by(data$partyID, data$j, function(x) sum(x == "Democrat", na.rm = TRUE))
  G$abs_prop_diff <- abs(0.5 - (G$n_r / (G$n_r + G$n_d))) * 2

  G$mean <- by(data$y, data$j, mean)
  G$mean_centered <- G$mean - mean(G$mean)

  return(G)

}

G2016 <- get_parameters(posterior_cces_2016, L2016, "CCES 2016")
G2016D <- get_parameters(posterior_cces_2016D, L2016D, "CCES 2016\nDemocrats")
G2016R <- get_parameters(posterior_cces_2016R, L2016R, "CCES 2016\nRepublicans")

G2012 <- get_parameters(posterior_cces_2012, L2012, "CCES 2012")
G2012D <- get_parameters(posterior_cces_2012D, L2012D, "CCES 2012\nDemocrats")
G2012R <- get_parameters(posterior_cces_2012R, L2012R, "CCES 2012\nRepublicans")

G2008 <- get_parameters(posterior_cces_2008, L2008, "CCES 2008")
G2008D <- get_parameters(posterior_cces_2008D, L2008D, "CCES 2008\nDemocrats")
G2008R <- get_parameters(posterior_cces_2008R, L2008R, "CCES 2008\nRepublicans")

G <- rbind(G2016, G2012, G2008)


# Appendix A
# Correlations between the standard deviation and AM-scaling model errors (sigma_j)
cor(G$sd[G$year == "CCES 2008"], G$sigma[G$year == "CCES 2008"])
cor(G$sd[G$year == "CCES 2012"], G$sigma[G$year == "CCES 2012"])
cor(G$sd[G$year == "CCES 2016"], G$sigma[G$year == "CCES 2016"])

# Standard deviation as estimated separately
# among Democrats and Republicans
cor(c(G2008D$sd, G2012D$sd, G2016D$sd),
    c(G2008R$sd, G2012R$sd, G2016R$sd))
cor(G2008R$sd, G2008D$sd)
cor(G2012R$sd, G2012D$sd)
cor(G2016R$sd, G2016D$sd)

# Correlation between sigma/SD as estimated separately
# among Democrats and Republicans
cor(c(G2008D$sigma, G2012D$sigma, G2016D$sigma),
    c(G2008R$sigma, G2012R$sigma, G2016R$sigma))
cor(G2008D$sigma, G2008R$sigma)
cor(G2012D$sigma, G2012R$sigma)
cor(G2016D$sigma, G2016R$sigma)

##### TABLE A1
model.sd <- glm(sd ~ abs_prop_diff + year, data = G)
model.sigma <- glm(sigma ~ abs_prop_diff + year, data = G)

stargazer(model.sd, model.sigma,
          title = "",
          style = "apsr",
          column.sep.width = "0pt",
          star.cutoffs = c(0.05, 0.01, 0.001),
          dep.var.labels.include = TRUE,
          model.names = FALSE,
          omit.stat = c("ll", "aic"),
          column.labels = c("Model", "Model"),
          covariate.labels = c("Partisan respondent gap",
                               "Year: 2012",
                               "Year: 2016",
                               "Intercept"))



##### CCES 2016

G2016$display_name <- FALSE
G2016$display_name[G2016$name %in% c("Donald Trump", "Hillary Clinton",
                                     "Barack Obama",
                                     "Democratic Party", "Republican Party")] <- TRUE

G2016$trump <- ifelse(G2016$name == "Donald Trump", "Trump", "Not Trump")

G2016 <- G2016[order(G2016$sigma), ]
row.names(G2016) <- 1:nrow(G2016)
G2016$yaxis <- nrow(G2016):1

gap2016 <- 2
G2016$yaxis <- rank(-G2016$sigma)
for(i in which(G2016$display_name)) {
  G2016$yaxis[G2016$yaxis > G2016$yaxis[i]] <- G2016$yaxis[G2016$yaxis > G2016$yaxis[i]] + gap2016
  G2016$yaxis[G2016$yaxis < G2016$yaxis[i]] <- G2016$yaxis[G2016$yaxis < G2016$yaxis[i]] - gap2016
}

G2016$side <- "Right"
G2016$side[G2016$sigma > 1] <- "Left"

G2016_plot <- ggplot(G2016, aes(y = yaxis, x = sigma, size = display_name,
                                shape = display_name, color = display_name, fill = trump)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 9, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.75, 1, 1.25, 1.5),
                     labels = c("0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("("*sigma*")"), title = "2016") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.3) +
  geom_point(stroke = 0.4) +
  geom_text(data = subset(G2016, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 2.75, color = "black") +
  geom_text(data = subset(G2016, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 2.75, color = "black") +
  scale_size_manual(values = c(0, 2.25)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_fill_manual(values = c("white", "pink")) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

G2016_plot_all_names <- ggplot(G2016, aes(y = yaxis, x = sigma, size = display_name,
                                          shape = display_name, color = display_name, fill = trump)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.75, 1, 1.25, 1.5),
                     labels = c("0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "CCES 2016") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.25) +
  geom_point(stroke = 0.4) +
  geom_text(data = subset(G2016, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 3, color = "black") +
  geom_text(data = subset(G2016, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 3, color = "black") +
  geom_text(data = subset(G2016, display_name == FALSE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.01, hjust = 0, size = 1.1, color = "black") +
  geom_text(data = subset(G2016, display_name == FALSE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.01, hjust = 1, size = 1.1, color = "black") +
  scale_size_manual(values = c(0, 2.25)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_fill_manual(values = c("white", "pink")) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

# "Among 156 national- and state-level political actors, the error in
# ideological placement for Trump is..."
# Estimates of sigma for Trump, the Democratic and Republican parties,
# and Clinton
round(G2016$sigma[G2016$name == "Donald Trump"], 2)
round(G2016$sigma[G2016$name == "Democratic Party"], 2)
round(G2016$sigma[G2016$name == "Republican Party"], 2)
round(G2016$sigma[G2016$name == "Hillary Clinton"], 2)

# "Results from these models show that although Republicans exhibited somewhat
# less uncertainty about Trump’s ideology than Democrats..."
# and APPENDIX E
# Sigma and rank for Trump among Republicans
round(G2016R$sigma[G2016R$name == "Donald Trump"], 2)
rank(G2016R$sigma)[which(G2016R$name == "Donald Trump")]

# Sigma and rank for Trump among Democrats
round(G2016D$sigma[G2016D$name == "Donald Trump"], 2)
rank(G2016D$sigma)[which(G2016D$name == "Donald Trump")]


# 2016 DEMOCRAT RESPONDENTS ONLY
G2016D$display_name <- FALSE
G2016D$display_name[G2016D$name %in% c("Donald Trump")] <- TRUE

G2016D$trump <- ifelse(G2016D$name == "Donald Trump", "Trump", "Not Trump")

G2016D <- G2016D[order(G2016D$sigma), ]
row.names(G2016D) <- 1:nrow(G2016D)
G2016D$yaxis <- nrow(G2016D):1

gap2016 <- 3
G2016D$yaxis <- rank(-G2016D$sigma)
for(i in which(G2016D$display_name)) {
  G2016D$yaxis[G2016D$yaxis > G2016D$yaxis[i]] <- G2016D$yaxis[G2016D$yaxis > G2016D$yaxis[i]] + gap2016
  G2016D$yaxis[G2016D$yaxis < G2016D$yaxis[i]] <- G2016D$yaxis[G2016D$yaxis < G2016D$yaxis[i]] - gap2016
}

G2016D$side <- "Right"
G2016D$side[G2016D$sigma > 1] <- "Left"

G2016D_plot <- ggplot(G2016D, aes(y = yaxis, x = sigma, size = display_name,
                                  shape = display_name, color = display_name, fill = trump)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 9, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.5, 0.75, 1, 1.25, 1.5, 1.75),
                     labels = c("0.5", "0.75", "1", "1.25", "1.5", "1.75")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "Democratic identifiers") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.2) +
  geom_point(data = subset(G2016D, display_name == TRUE), stroke = 0.4) +
  geom_text(data = subset(G2016D, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 3, color = "black") +
  geom_text(data = subset(G2016D, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 3, color = "black") +
  scale_size_manual(values = c("TRUE" = 2.25, "FALSE" = 0.8)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_fill_manual(values = c("white", "pink")) +
  scale_color_manual(values = c("black", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))


# 2016 REPUBLICAN RESPONDENTS ONLY
G2016R$display_name <- FALSE
G2016R$display_name[G2016R$name %in% c("Donald Trump")] <- TRUE

G2016R$trump <- ifelse(G2016R$name == "Donald Trump", "Trump", "Not Trump")

G2016R <- G2016R[order(G2016R$sigma), ]
row.names(G2016R) <- 1:nrow(G2016R)
G2016R$yaxis <- nrow(G2016R):1

gap2016 <- 3
G2016R$yaxis <- rank(-G2016R$sigma)
for(i in which(G2016R$display_name)) {
  G2016R$yaxis[G2016R$yaxis > G2016R$yaxis[i]] <- G2016R$yaxis[G2016R$yaxis > G2016R$yaxis[i]] + gap2016
  G2016R$yaxis[G2016R$yaxis < G2016R$yaxis[i]] <- G2016R$yaxis[G2016R$yaxis < G2016R$yaxis[i]] - gap2016
}

G2016R$side <- "Right"
G2016R$side[G2016R$sigma > 1] <- "Left"

G2016R_plot <- ggplot(G2016R, aes(y = yaxis, x = sigma, size = display_name,
                                  shape = display_name, color = display_name, fill = trump)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 9, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.5, 0.75, 1, 1.25, 1.5, 1.75),
                     labels = c("0.5", "0.75", "1", "1.25", "1.5", "1.75")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "Republican identifiers") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.2) +
  geom_point(data = subset(G2016R, display_name == TRUE), stroke = 0.4) +
  geom_text(data = subset(G2016R, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 3, color = "black") +
  geom_text(data = subset(G2016R, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 3, color = "black") +
  scale_size_manual(values = c("TRUE" = 2.25, "FALSE" = 0.8)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_fill_manual(values = c("white", "pink")) +
  scale_color_manual(values = c("black", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))


pdf("Graphs/Figure_A4-RAW.pdf", 5, 5, useDingbats = FALSE)
grid.arrange(G2016D_plot, G2016R_plot, ncol = 2)
dev.off()


##### CCES 2012

G2012$display_name <- FALSE
G2012$display_name[G2012$name %in% c("Mitt Romney", "Barack Obama",
                                     "Democratic Party", "Republican Party")] <- TRUE

G2012 <- G2012[order(G2012$sigma), ]
row.names(G2012) <- 1:nrow(G2012)
G2012$yaxis <- nrow(G2012):1

gap2012 <- 3
G2012$yaxis <- rank(-G2012$sigma)
for(i in which(G2012$display_name)) {
  G2012$yaxis[G2012$yaxis > G2012$yaxis[i]] <- G2012$yaxis[G2012$yaxis > G2012$yaxis[i]] + gap2012
  G2012$yaxis[G2012$yaxis < G2012$yaxis[i]] <- G2012$yaxis[G2012$yaxis < G2012$yaxis[i]] - gap2012
}

G2012$side <- "Right"
G2012$side[G2012$sigma > 1] <- "Left"

G2012_plot <- ggplot(G2012, aes(y = yaxis, x = sigma, size = display_name,
                                shape = display_name, color = display_name)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 9, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.75, 1, 1.25, 1.5),
                     labels = c("0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "2012") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.3) +
  geom_point(fill = "white", stroke = 0.4) +
  geom_text(data = subset(G2012, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 2.75, color = "black") +
  geom_text(data = subset(G2012, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 2.75, color = "black") +
  scale_size_manual(values = c(0, 2.25)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

G2012_plot_all_names <- ggplot(G2012, aes(y = yaxis, x = sigma, size = display_name,
                                          shape = display_name, color = display_name)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.75, 1, 1.25, 1.5),
                     labels = c("0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "CCES 2012") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.25) +
  geom_point(fill = "white", stroke = 0.4) +
  geom_text(data = subset(G2012, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 3, color = "black") +
  geom_text(data = subset(G2012, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 3, color = "black") +
  geom_text(data = subset(G2012, display_name == FALSE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.01, hjust = 0, size = 1.1, color = "black") +
  geom_text(data = subset(G2012, display_name == FALSE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.01, hjust = 1, size = 1.1, color = "black") +
  scale_size_manual(values = c(0, 2.25)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))



##### CCES 2008

G2008$display_name <- FALSE
G2008$display_name[G2008$name %in% c("George W. Bush", "Barack Obama",
                                     "John McCain",
                                     "Democratic Party", "Republican Party")] <- TRUE

G2008 <- G2008[order(G2008$sigma), ]
row.names(G2008) <- 1:nrow(G2008)
G2008$yaxis <- nrow(G2008):1

gap2008 <- 1.75
G2008$yaxis <- rank(-G2008$sigma)
for(i in which(G2008$display_name)) {
  G2008$yaxis[G2008$yaxis > G2008$yaxis[i]] <- G2008$yaxis[G2008$yaxis > G2008$yaxis[i]] + gap2008
  G2008$yaxis[G2008$yaxis < G2008$yaxis[i]] <- G2008$yaxis[G2008$yaxis < G2008$yaxis[i]] - gap2008
}

G2008$side <- "Right"
G2008$side[G2008$sigma > 1] <- "Left"

G2008_plot <- ggplot(G2008, aes(y = yaxis, x = sigma, size = display_name,
                                shape = display_name, color = display_name)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 9, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.5, 0.75, 1, 1.25, 1.5),
                     labels = c("0.5", "0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("("*sigma*")"), title = "2008") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.3) +
  geom_point(fill = "white", stroke = 0.4) +
  geom_text(data = subset(G2008, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 2.75, color = "black") +
  geom_text(data = subset(G2008, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 2.75, color = "black") +
  scale_size_manual(values = c(0, 2.75)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))

G2008_plot_all_names <- ggplot(G2008, aes(y = yaxis, x = sigma, size = display_name,
                                          shape = display_name, color = display_name)) +
  my.theme(borderless = 2, bordersize = 1, base_size = 10, tick_colour = "white",
           grid.x_colour = "grey85", grid.y_colour = "grey85") +
  scale_y_continuous(breaks = c(), expand = c(0.02, 0.02)) +
  scale_x_continuous(breaks = c(0.5, 0.75, 1, 1.25, 1.5),
                     labels = c("0.5", "0.75", "1", "1.25", "1.5")) +
  labs(y = "", x = expression("Ideological placement error ("*sigma*")"), title = "CCES 2008") +
  geom_segment(aes(y = yaxis, yend = yaxis, x = lower90, xend = upper90), lineend = "round", size = 0.25) +
  geom_point(fill = "white", stroke = 0.4) +
  geom_text(data = subset(G2008, display_name == TRUE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.04, hjust = 0, size = 3, color = "black") +
  geom_text(data = subset(G2008, display_name == TRUE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.04, hjust = 1, size = 3, color = "black") +
  geom_text(data = subset(G2008, display_name == FALSE & side == "Right"), aes(label = name, x = upper90), nudge_x = 0.01, hjust = 0, size = 1.75, color = "black") +
  geom_text(data = subset(G2008, display_name == FALSE & side == "Left"), aes(label = name, x = lower90), nudge_x = -0.01, hjust = 1, size = 1.75, color = "black") +
  scale_size_manual(values = c(0.8, 2.25)) +
  scale_shape_manual(values = c(15, 21)) +
  scale_color_manual(values = c("grey40", "black")) +
  theme(legend.position = "none",
        panel.background = element_rect(fill = "grey97", color = "white"),
        plot.margin = unit(c(0.5, 0.5, 0.5, -0.5), "lines"))


pdf("Graphs/Figure_3-RAW.pdf", 6.5, 5, useDingbats = FALSE)
grid.arrange(G2008_plot, G2012_plot, G2016_plot, ncol = 3)
dev.off()


pdf("Graphs/Figure_A5-RAW.pdf", 4, 8, useDingbats = FALSE)
G2016_plot_all_names
dev.off()

pdf("Graphs/Figure_A6-RAW.pdf", 4, 8, useDingbats = FALSE)
G2012_plot_all_names
dev.off()

pdf("Graphs/Figure_A7-RAW.pdf", 4, 8, useDingbats = FALSE)
G2008_plot_all_names
dev.off()

